package com.gnerv.sylvanas.business.manage.controller;

import com.gnerv.sylvanas.framework.common.model.RestResult;
import com.gnerv.sylvanas.business.manage.logic.ApiLogicService;
import com.gnerv.sylvanas.business.manage.model.dto.ApiCreateDTO;
import com.gnerv.sylvanas.business.manage.model.dto.ApiDTO;
import com.gnerv.sylvanas.business.manage.model.dto.ApiRoleDTO;
import com.gnerv.sylvanas.business.manage.model.dto.ApiUpdateDTO;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Gnerv LiGen
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping(value = "/api/manage/api")
public class ApiController {

    private final ApiLogicService apiLogicService;

    @PostMapping(value = "")
    public RestResult<Void> create(@RequestBody ApiCreateDTO apiCreateDTO) {
        return apiLogicService.create(apiCreateDTO);
    }

    @PostMapping(value = "/bind/role")
    public RestResult<Void> bindRole(@RequestBody ApiRoleDTO apiRoleDTO) {
        return apiLogicService.bindRole(apiRoleDTO);
    }

    @DeleteMapping(value = "")
    public RestResult<Void> deleteByUkId(@RequestParam(value = "ukId") String ukId) {
        return apiLogicService.deleteByUkId(ukId);
    }

    @PutMapping(value = "")
    public RestResult<Void> updateByUkId(@RequestBody ApiUpdateDTO apiUpdateDTO) {
        return apiLogicService.updateByUkId(apiUpdateDTO);
    }

    @GetMapping(value = "")
    public RestResult<ApiDTO> selectByUkId(@RequestParam(value = "ukId") String ukId) {
        return apiLogicService.selectByUkId(ukId);
    }

    @GetMapping(value = "/list")
    public RestResult<List<ApiDTO>> selectListByName(@RequestParam(required = false, value = "name") String name,
                                                     @RequestParam(required = false, value = "page", defaultValue = "1") int page,
                                                     @RequestParam(required = false, value = "size", defaultValue = "30") int size) {
        return apiLogicService.selectListByName(name, page, size);
    }

    @GetMapping(value = "/list/group")
    public RestResult<List<ApiDTO>> selectListByGroupUkId(@RequestParam(required = false, value = "groupUkId") String groupUkId,
                                                          @RequestParam(required = false, value = "page", defaultValue = "1") int page,
                                                          @RequestParam(required = false, value = "size", defaultValue = "30") int size) {
        return apiLogicService.selectListByGroupUkId(groupUkId, page, size);
    }

    @GetMapping(value = "/list/role")
    public RestResult<List<ApiDTO>> selectListByRoleUkId(@RequestParam(value = "roleUkId") String roleUkId) {
        return apiLogicService.selectListByRoleUkId(roleUkId);
    }

}
